<?php

class Permission extends FMIS_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->model('faculty_member_permission_model');
        $this->load->model('faculty_permission_model');
    }

	public function edit($id)
    {
        $permissions = $this->faculty_permission_model->getAll();
        $permittedFunctions = $this->faculty_member_permission_model->getAllByMemberID($id);

        //$permissions = count($permissions) == 0 ? 'its zero' :'we\'re good';

        $data = array(
            'memberID' => $id,
            'permissions' => $permissions,
            'permissionsList' => $permittedFunctions
        );
        $data = array_merge($this->session->all_userdata(), $data);
        //$this->load->view('permission/edit',$data);
        echo $this->load->view('permission/edit',$data,true);
        //echo $this->setup_view('permission/edit',$data,true);
        //echo '<div class="loading">'.$id.'<img src="http://lorempixel.com/140/140" alt="Loading..." /></div>';
    }

    public function save($id)
    {

        $data = array();
        $permissions = $this->faculty_permission_model->getAll();
        $status = false;
        foreach($permissions as $permission)
        {
            //echo $this->input->post($permission['Permissions']);

            if($this->input->post($permission['Permissions']) == 'true')
            {
                //echo 'haha ';
                $resultInsert = $this->faculty_member_permission_model->insert($id, $permission['ID']);

                if($resultInsert == true)
                    $data[$permission['Permissions']] = $permission['ID'];

                if($status == false)
                    $status = $resultInsert;
            }
            else
            {
               $resultDelete = $this->faculty_member_permission_model->delete($id, $permission['ID']);

                if($resultDelete == true)
                    $data[$permission['Permissions']] = $permission['ID'];

                if($status == false)
                    $status = $resultDelete;
            }
        }

        $data['status'] = $status;
        echo json_encode($data);


    }
}